<template>
  <XtxDialog
    :title="isEdit ? '编辑收货地址' : '添加收货地址'"
    v-model:visible="dialogVisible"
  >
    <div class="address-edit">
      <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input
              class="input"
              v-model="formData.receiver"
              placeholder="请输入收货人"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input
              class="input"
              v-model="formData.contact"
              placeholder="请输入手机号"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity
              :fullLocation="formData.fullLocation"
              @adress-change="addressChange"
              placeholder="请选择所在地区"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input
              class="input"
              v-model="formData.address"
              placeholder="请输入详细地址"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input
              class="input"
              v-model="formData.postalCode"
              placeholder="请输入邮政编码"
            />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input
              class="input"
              v-model="formData.addressTags"
              placeholder="请输入地址标签，逗号分隔"
            />
          </div>
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton
        type="gray"
        style="margin-right: 20px"
        @click="dialogVisible = false"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { reactive, ref } from 'vue'
import { addAddress } from '@/api/order'
import Message from '@/components/Message'
export default {
  name: 'AddressEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    const dialogVisible = ref(false)

    // open():做添加
    // open(原地址对象):做编辑
    const open = (form = {}) => {
      // 清空表单
      if (!form.id) {
        for (const key in formData) {
          // 除isDefault之外全部清空
          if (key !== 'isDefault') {
            formData[key] = ''
          }
        }
      }
      dialogVisible.value = true
    }

    // 定义表单数据
    const formData = reactive({
      receiver: '', // 用户
      contact: '', // 手机号
      provinceCode: '', // 省编码
      cityCode: '', // 市编码
      countyCode: '',
      address: '', // 详细地址
      postalCode: '', // 邮政编码
      addressTags: '', // 地址标签
      isDefault: 0, // 是否默认
      fullLocation: ''
    })

    // 监听地址组件的地址事件,获取数据填充表单
    const addressChange = (address) => {
      formData.provinceCode = address.provinceCode // 省
      formData.cityCode = address.cityCode // 市
      formData.countyCode = address.countyCode // 区
      // 拼接地址
      formData.fullLocation = address.provinceName + address.cityName + address.countyName
    }
    const submit = () => {
      if (props.isEdit) {

      } else {
        // 添加
        doAdd()
      }
    }
    // 添加
    const doAdd = () => {
      // 拼接用户输入的地址，传入
      const fullLocation = formData.fullLocation + formData.address
      const params = { ...formData, ...{ fullLocation } }
      addAddress(params).then(data => {
        // 提示成功
        Message({ type: 'success', text: '添加成功' })
        // 关闭弹窗
        dialogVisible.value = false
        // 把添加成功的数据传给父组件(补充id属性)
        params.id = data.result.id
        emit('on-success', params)
      }).catch(() => { })
    }
    return { dialogVisible, open, submit, formData, addressChange }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
